package com.antler.smt.admin.service.impl;

import com.antler.smt.admin.mapper.*;
import com.antler.smt.admin.utils.ShiroUtils;
import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.entity.PurchaserAccountEntity;
import com.antler.smt.entity.PurchaserChargeEntity;
import com.antler.smt.entity.PurchaserEntity;
import com.antler.smt.entity.UserEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.GetPurchaserListReq;
import com.antler.smt.request.admin.GetRequirementListReq;
import com.antler.smt.request.admin.PurchaserAccountReq;
import com.antler.smt.request.base.BasePageReq;
import com.antler.smt.response.admin.*;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.PurchaserService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.LogUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购商列表实现接口
 *
 * @author zhangyi
 * @date 2018/3/29 15:27
 */
@Service
public class PurchaserServiceImpl implements PurchaserService {


    /**
     * 日志
     */
    private static Logger log = LogUtil.get();


    @Autowired
    private PurchaserInfoMapper purchaserInfoMapper;

    @Autowired
    private PurchaserEntityMapper purchaserEntityMapper;

    @Autowired
    private RequirementInfoMapper requirementInfoMapper;

    @Autowired
    private PurchaserChargeInfoMapper purchaserChargeInfoMapper;

    @Autowired
    private PurchaserAccountInfoMapper purchaserAccountInfoMapper;

    @Autowired
    private PurchaserAccountEntityMapper purchaserAccountEntityMapper;

    /**
     * 获取其采购商发布需求列表
     *
     * @param request 分页信息
     * @return
     */
    @Override
    public Result<Page<GetRequirementListResp>> getPurchaserRequirementList(GetRequirementListReq request) {
        PageHelper.startPage(request.getPageCurrent(), request.getPageSize());
        Page<GetRequirementListResp> listPurchaserRequirementInfo = requirementInfoMapper.listRequirementInfo(request);
        PageInfo<GetRequirementListResp> PurchaserRequirementListPageInfo = null;
        if (listPurchaserRequirementInfo != null) {
            PurchaserRequirementListPageInfo = new PageInfo(listPurchaserRequirementInfo);
        }
        Result result = new Result(PurchaserRequirementListPageInfo);
        return result;
    }

    /**
     *  获取采购商待审核结果列表 认证审核状态 0未申请 1已申请未审核 2审核通过 3审核未通过
     *
     * @param request
     * @return
     */
    @Override
    public Result<Page<GetPurchaserListResp>> getPurchaserAuditResultList(GetPurchaserListReq request) {
        Result result = null;
        PageHelper.startPage(request.getPageCurrent(), request.getPageSize());
        Page<GetPurchaserListResp> listPurchaserAuditResult =
                purchaserInfoMapper.getPurchaserAuditResultList(request.getAuditResult());
        PageInfo<GetPurchaserListResp> PurchaserAuditResultListPageInfo = null;
        if (listPurchaserAuditResult != null) {
            PurchaserAuditResultListPageInfo = new PageInfo(listPurchaserAuditResult);
        }
        result = new Result(PurchaserAuditResultListPageInfo);
        return result;
    }

    /**
     *  获取指定的采购商实体
     *
     * @param id
     * @return
     */
    @Override
    public Result<PurchaserEntity> getPurchaserInfoById(Long id) {
        Result result = null;

        PurchaserEntity purchaserEntity = purchaserEntityMapper.selectByPrimaryKey(id);

        result = new Result(purchaserEntity);

        return result;
    }

    /**
     *  采购商认证审核
     *  认证审核状态 0未申请 1已申请未审核 2审核通过 3审核未通过
     *
     * @param id
     * @param auditResult 审核结果
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result<ResultCodeEnum> updatePurchaserAuditResult(Long id,Integer auditResult) {
        PurchaserEntity purchaserEntity =  new PurchaserEntity();

        //设置服务商的id
        purchaserEntity.setId(id);

        //设置服务商的审核状态
        purchaserEntity.setAuditResult(auditResult);

        //设置审核时间
        purchaserEntity.setAuditTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //设置更新时间
        purchaserEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //获取用户实体
        UserEntity userEntity = ShiroUtils.getUserEntity();
        if(userEntity!=null){
            //认证审核用户id
            purchaserEntity.setAuditUserId(userEntity.getId());
        }

        int i = purchaserEntityMapper.updateByPrimaryKeySelective(purchaserEntity);

        if(0 == i){
            return new Result(ResultCodeEnum.APPEND_DATA_ERROR);
        }else{

            return new Result(ResultCodeEnum.OPERATION_SUCCESS);
        }
    }

    /**
     *  获取采购商充值待审核列表
     *
     * @return
     */
    @Override
    public Result<PageInfo<PurchaserChargeEntity>> getPurchaserWaitAuditList(BasePageReq request){
        Result result;
        PageHelper.startPage(request.getPageCurrent(), request.getPageSize());

        Page<PurchaserChargeEntity> waitAuditList = purchaserChargeInfoMapper.getWaitAuditList();
        for(int i = 0; i < waitAuditList.size(); i++){
            PurchaserChargeEntity purchaserList = waitAuditList.get(i);
            //获取其订单数量
            GetRequirementListReq requirementListReq = new GetRequirementListReq();
            requirementListReq.setPurchaserId(purchaserList.getPurchaserId());

            List<GetRequirementListResp> requirementList = requirementInfoMapper.listRequirementInfo(requirementListReq);

            //获取订单数量
            long size = requirementList.size();
            PurchaserAccountEntity purchaserAccount =
                    purchaserAccountInfoMapper.selectPurchaserAccountByMemberId(purchaserList.getMemberId());

            waitAuditList.get(i).setVipAndCount("v"+purchaserAccount.getVipLevel()
                    +"/"+purchaserAccount.getCashCoupon()+"张");

            waitAuditList.get(i).setOrderCount(Integer.parseInt(String.valueOf(size)));

            //后台实际审核的总金额
            Long totalMoney = purchaserChargeInfoMapper.getTotalMoney(purchaserList.getPurchaserId());
            waitAuditList.get(i).setTotalMoney(totalMoney);
            //添加最后一次审核的金额
            Long lastAuditResult = purchaserChargeInfoMapper.getLastAuditResult(purchaserList.getPurchaserId());
            waitAuditList.get(i).setLastMoney(lastAuditResult);
        }

        if (waitAuditList != null) {
            PageInfo<PurchaserChargeEntity> listPageInfo = new PageInfo(waitAuditList);
            result = new Result(listPageInfo);
        } else {
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR, "查询采购商待审核列表数据为空！");
        }
        return result;
    }

    /**
     * 修改采购商信息
     *
     * @param req
     * @param purchaserId
     * @return
     */
    @Override
    public Result<ResultCodeEnum> editPurchaserAccount(PurchaserAccountReq req, Long purchaserId) {
        Result result = null;

        PurchaserAccountEntity purchaserAccountEntity = new PurchaserAccountEntity();

        PurchaserEntity purchaserEntity = purchaserEntityMapper.selectByPrimaryKey(purchaserId);
        PurchaserAccountEntity purchaserAccount =
                purchaserAccountInfoMapper.selectPurchaserAccountByMemberId(purchaserEntity.getMemberId());

        purchaserAccountEntity.setId(purchaserAccount.getId());
        purchaserAccountEntity.setVipLevel(req.getVipLevel());
        purchaserAccountEntity.setCashCoupon(req.getCashCoupon());
        purchaserAccountEntity.setBankInfo(req.getBankInfo());
        purchaserAccountEntity.setMoneyLeft(req.getMoneyLeft());
        int i = purchaserAccountEntityMapper.updateByPrimaryKeySelective(purchaserAccountEntity);
        result = (0==i?new Result(ResultCodeEnum.MODIFY_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));
        return result;
    }

}
